#!/bin/bash

# shellcheck source=meta-facebook/meta-harma/recipes-phosphor/gpio/phosphor-gpio-monitor/device-util
source /usr/libexec/phosphor-gpio-monitor/device-util

read_i2c_dev() {
  local bus="$1"
  local addr="$2"

  for _ in {1..3}; do
    if /usr/sbin/i2cget -f -y "$bus" "$addr" 2>/dev/null; then
      return 0
    fi
  done
  return 1
}

probe_dev() {

  # MB Voltage Monitor
  # Set adc128d818 in2 max. value to prevent
  # psusensor[1011]: Failure assert/deassert
  # from continuously triggering.
  i2cset -f -y 29 0x1d 0x2e 0xff
  i2cset -f -y 29 0x1f 0x2e 0xff

  if read_i2c_dev 9 0x71; then
    # EVT PTTV or SITV
    echo pca9546 0x71 > /sys/bus/i2c/devices/i2c-9/new_device
  elif read_i2c_dev 11 0x71; then
    # DVT PTTV or SITV
    echo pca9546 0x71 > /sys/bus/i2c/devices/i2c-11/new_device
  else
    # Aegis board
    return
  fi


  # Set adc128d818 in2 max. value to prevent
  # psusensor[1011]: Failure assert/deassert
  # from continuously triggering.
  i2cset -f -y 36 0x1d 0x2e 0xff
  echo adc128d818 0x1d > /sys/bus/i2c/devices/i2c-36/new_device
  echo ina238 0x44 > /sys/bus/i2c/devices/i2c-36/new_device
  echo ina238 0x45 > /sys/bus/i2c/devices/i2c-36/new_device
  echo MCP9600 0x60 > /sys/bus/i2c/devices/i2c-34/new_device
  echo MCP9600 0x62 > /sys/bus/i2c/devices/i2c-34/new_device
  echo MCP9600 0x63 > /sys/bus/i2c/devices/i2c-34/new_device
  echo MCP9600 0x64 > /sys/bus/i2c/devices/i2c-34/new_device
  echo MCP9600 0x65 > /sys/bus/i2c/devices/i2c-34/new_device
  echo MCP9600 0x66 > /sys/bus/i2c/devices/i2c-34/new_device
  echo MCP9600 0x67 > /sys/bus/i2c/devices/i2c-34/new_device
  echo MCP9600 0x64 > /sys/bus/i2c/devices/i2c-35/new_device
  echo MCP9600 0x65 > /sys/bus/i2c/devices/i2c-35/new_device
  echo MCP9600 0x66 > /sys/bus/i2c/devices/i2c-35/new_device
  echo tmp75 0x49 > /sys/bus/i2c/devices/i2c-36/new_device
  echo tmp75 0x4A > /sys/bus/i2c/devices/i2c-36/new_device
  echo tmp75 0x4B > /sys/bus/i2c/devices/i2c-36/new_device
  echo tmp75 0x4C > /sys/bus/i2c/devices/i2c-36/new_device
  echo tmp75 0x49 > /sys/bus/i2c/devices/i2c-37/new_device
  echo tmp75 0x4A > /sys/bus/i2c/devices/i2c-37/new_device
  echo tmp75 0x4B > /sys/bus/i2c/devices/i2c-37/new_device
  echo tmp75 0x4C > /sys/bus/i2c/devices/i2c-37/new_device
}

set_hsc_sample_rate() {
  if read_i2c_dev 4 0x44; then
     # set ltc4287 sample to 4096/1.16s
     i2cset -f -y 4 0x44 0xD9 0x8b
  fi
}

# If the motherboard power recovers before the GPIO monitor starts,
# we can't detect the standby power transition from low to high.
# Thus, we add mb_power_lose_reprobe to ensure the motherboard device is ready
mb_power_lose_reprobe

probe_dev
set_hsc_sample_rate
exit 0
